package com.iris.live.services.report.sources;

import com.iris.live.services.data.repositories.ReportRepository;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by HangLiu on 2017/1/11.
 */
@Component("SmToBWeekReportSource")
public class SmToBWeekReportSource extends AbstractReportSourceService {
    private static final Logger logger = LoggerFactory.getLogger(LSHROSWeekReportSource.class);
    private static final String[] lastType = {
            "-oneJan",
            "-oneFeb",
            "-oneMar",
            "-oneApr",
            "-oneMay",
            "-oneJun",
            "-oneJul",
            "-oneAug",
            "-oneSep",
            "-oneOct",
            "-oneNov",
            "-oneDeco",
            ""
    };

    private static final String[] indicators = {

            "Volume Pillar-C-W205-C200", "Volume Pillar-C-W205-C200 4MATIC", "Volume Pillar-C-W205-C300", "Volume Pillar-C-V205-C180 L",
            "Volume Pillar-C-V205-C200 L", "Volume Pillar-C-V205-C200 L 4MATIC", "Volume Pillar-C-V205-C260 L", "Volume Pillar-C-V205-C300 L",
            "Volume Pillar-C-V205-C350 e L",
            "formular",
            "Volume Pillar-E-W213-E 200", "Volume Pillar-E-W213-E 200 4MATIC", "Volume Pillar-E-V213-E 300", "Volume Pillar-E-V213-E200 L",
            "Volume Pillar-E-V213-E200 L 4MATIC", "Volume Pillar-E-V213-E300 L", "Volume Pillar-E-V213-E320 L 4MATIC", "Volume Pillar-E-V212-E180 L 2nd FL",
            "Volume Pillar-E-V212-E200 L 2nd FL", "Volume Pillar-E-V212-E260 L 2nd FL", "Volume Pillar-E-V212-E320 L 2nd FL", "Volume Pillar-E-V212-E400 L 2nd FL",
            "Volume Pillar-E-V212-E260 L FL", "Volume Pillar-E-V212-E300 L FL", "Volume Pillar-E-V212-E400 L FL",
            "formular",
            "Volume Pillar-S-V222-S320 L", "Volume Pillar-S-V222-S400 L", "Volume Pillar-S-V222-S400 L 4MATIC", "Volume Pillar-S-V222-S400 L Hybrid",
            "Volume Pillar-S-V222-S500 e L", "Volume Pillar-S-V222-S500 L", "Volume Pillar-S-V222-S500 4MATIC", "Volume Pillar-S-V222-S600 L",
            "Volume Pillar-S-V221-S350 L 4MATIC", "Volume Pillar-S-V221-S400 L Hybrid", "Volume Pillar-S-V221-S600 L",
            "formular",
            "formular",
            "SUV-GLA-X156-GLA 200", "SUV-GLA-X156-GLA 220 4MATIC", "SUV-GLA-X156-GLA 260 4MATIC",
            "formular",
            "SUV-GLA(CBU)-X156-GLA 200", "SUV-GLA(CBU)-X156-GLA 260",
            "formular",
            "SUV-GLC-X253-GLC 200 4MATIC", "SUV-GLC-X253-GLC 260 4MATIC", "SUV-GLC-X253-GLC 300 4MATIC",
            "formular",
            "SUV-GLK-X204-GLK 200", "SUV-GLK-X204-GLK 260", "SUV-GLK-X204-GLK 300",
            "formular",
            "SUV-GLC Coupe-C253-GLC 200 4MATIC Coupe", "SUV-GLC Coupe-C253-GLC 260 4MATIC Coupe", "SUV-GLC Coupe-C253-GLC 300 4MATIC Coupe",
            "formular",
            "SUV-GLE-W166-GLE 300 4MATIC", "SUV-GLE-W166-GLE 320 4MATIC", "SUV-GLE-W166-GLE 350 4MATIC",
            "SUV-GLE-W166-GLE 400 4MATIC", "SUV-GLE-W166-GLE 450 4MATIC", "SUV-GLE-W166-GLE 500 e 4MATIC",
            "formular",
            "SUV-M-W166-ML320", "SUV-M-W166-ML350 CDI", "SUV-M-W166-ML400", "SUV-M-W166-ML350",
            "formular",
            "SUV-GLE Sport-C292-GLE 320 4MATIC Sport", "SUV-GLE Sport-C292-GLE 400 4MATIC Sport",
            "SUV-GLE Sport-C292-GLE 450 AMG 4MATIC Sport", "SUV-GLE Sport-C292-GLE 500 4MATIC Sport",
            "formular",
            "SUV-GLS-X166-GLS 350 4MATIC", "SUV-GLS-X166-GLS 400 4MATIC", "SUV-GLS-X166-GLS 500 4MATIC",
            "formular",
            "SUV-GL-X166-GL350 CDI 4MATIC", "SUV-GL-X166-GL400 4MATIC", "SUV-GL-X166-GL500 4MATIC", "SUV-GL-X164-GL450",
            "formular",
            "SUV-G-W463-G350", "SUV-G-W463-G500",
            "formular",
            "formular",
            "Compact Car-A-W176-A180", "Compact Car-A-W176-A200", "Compact Car-A-W176-A260",
            "formular",
            "Compact Car-B-W246-B180", "Compact Car-B-W246-B200", "Compact Car-B-W246-B260",
            "formular",
            "Compact Car-CLA-C117-CLA200", "Compact Car-CLA-C117-CLA220", "Compact Car-CLA-C117-CLA260",
            "formular",
            "formular",
            "Dream Car-C Estate-S205-C180 Estate", "Dream Car-C Estate-S205-C200 Estate", "Dream Car-C Estate-S205-C200 4MATIC Estate",
            "Dream Car-C Estate-S205-C300 Estate", "Dream Car-C Estate-S204-C200 Estate",
            "formular",
            "Dream Car-C Coupe-C205-C200 Coupe", "Dream Car-C Coupe-C205-C300 Coupe", "Dream Car-C Coupe-C204-C180 Coupe",
            "formular",
            "Dream Car-SLC-R172-SLC200", "Dream Car-SLC-R172-SLC300",
            "formular",
            "Dream Car-SLK-R172-SLK200", "Dream Car-SLK-R172-SLK350",
            "formular",
            "Dream Car-E Coupe-C207-E200 Coupe", "Dream Car-E Coupe-C207-E260 Coupe",
            "Dream Car-E Coupe-C207-E320 Coupe", "Dream Car-E Coupe-C207-E350 Coupe",
            "Dream Car-E Coupe-C207-E400 Coupe",
            "formular",
            "Dream Car-E Cabrio-A207-E260 Cabriolet", "Dream Car-E Cabrio-A207-E400 Cabriolet",
            "Dream Car-E Cabrio-A207-E350 Cabriolet", "Dream Car-E Cabrio-A207-E260 Cabriolet FL",
            "Dream Car-E Cabrio-A207-E400 Cabriolet FL",
            "formular",
            "Dream Car-CLS-C218-CLS260", "Dream Car-CLS-C218-CLS320",
            "Dream Car-CLS-C218-CLS400 4MATIC", "Dream Car-CLS-C218-CLS300",
            "Dream Car-CLS-C218-CLS350",
            "formular",
            "Dream Car-CLS BE-X218-CLS260 BE", "Dream Car-CLS BE-X218-CLS350 BE", "Dream Car-CLS BE-X218-CLS400 BE",
            "formular",
            "Dream Car-SL-R230-SL300", "Dream Car-SL-R231-SL350", "Dream Car-SL-R231-SL400",
            "formular",
            "Dream Car-S Coupe-C217-S400 4MATIC Coupe", "Dream Car-S Coupe-C217-S500 4MATIC Coupe",
            "formular",
            "Dream Car-CL-C216-CL600",
            "formular",
            "Dream Car-R-V251-R320", "Dream Car-R-V251-R400", "Dream Car-R-V251-R300",
            "Dream Car-R-V251-R350", "Dream Car-R-V251-R500",
            "formular",
            "formular",
            "MM-S-X222-MM S400", "MM-S-X222-MM S500", "MM-S-X222-MM S600",
            "formular",
            "formular",
            "AMG-A45-W176-A45", "AMG-GLA45-X156-GLA45", "AMG-CLA45-C117-CLA45", "AMG-C63-W205-C63",
            "AMG-C63 Dynamic-W204-C63 Dynamic", "AMG-C63 Sedan-W204-C63 Sedan", "AMG-C63 Coupe-C205-C63 Coupe",
            "AMG-C63 Coupe-C204-C63 Coupe", "AMG-SLK55-R171-SLK55", "AMG-AMG GT-C190-AMG GT", "AMG-AMG GT S-C190-AMG GT S",
            "AMG-CLS63-C218-CLS63", "AMG-GLE63 Sport-C292-GLE63 Sport", "AMG-S63 Coupe-C217-S63 Coupe", "AMG-S63-V222-S63",
            "AMG-S65-V222-S65", "AMG-S65-V221-S65", "AMG-SL63-R231-SL63", "AMG-SLS-C197-SLS", "AMG-G63-W463-G63",
            "AMG-G65-W463-G65", "AMG-GLE63-W166-GLE63", "AMG-ML63-W166-ML63", "AMG-GLS63-X166-GLS63", "AMG-GL63-X166-GL63",
            "formular",
            "formular",
            "formular",
            "formular",
            "formular"
    };

    @Autowired
    private ReportRepository reportRepository;

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) {
        SimpleDateFormat ssdf = new SimpleDateFormat("yyyy-MM-dd");
        Map values = getDataAboutWeekReport(sheet.getCode(), context.get("date").toString());
        String date = context.get("date").toString();
        List<Row> rows = new ArrayList<>();

        rows.add(new Row());

        Calendar c = Calendar.getInstance();
        try {
            c.setTime(ssdf.parse(date));
        } catch (Exception e) {
            logger.error("SM2B周报表日期转换异常", e);
        }

        String i = String.valueOf(c.get(Calendar.WEEK_OF_YEAR));
        Cell[] cells = new Cell[1];
        cells[0] = new Cell("LSH SM2b Purchase Report - Week " + i + "(" + date + ")", 1);
        rows.add(new Row(cells));

        Cell[] cell = new Cell[1];
        cell[0] = new Cell(sheet.getName(), 1);
        rows.add(new Row());

        //rows.add(new Row());
        rows.add(new Row());
        rows.add(new Row());
        rows.add(new Row());

        for (String indicator : indicators) {
            if (indicator == null || "".equals(indicator)) {
                rows.add(new Row());
            } else if (indicator.equals("formular")) {
                rows.add(new Row(true));
            } else {
                rows.add(new Row(getCells(values, indicator)));
            }
        }
        return rows;
    }

    private Cell[] getCells(Map values, String indicator) {
        Cell[] cells = new Cell[lastType.length];
        String value = null;
        for (int index = 0; index < lastType.length; index++) {
            if (!"".equals(lastType[index])) {
                Object o = values.get(indicator + lastType[index]);
                value = (o == null ? "0" : o).toString();
            } else {
                Double val = 0.0;
                for (int i = 12; i > 0; i--) {
                    Object o1 = values.get(indicator + lastType[index - i]);
                    val = val + Double.parseDouble((o1 == null ? "0" : o1).toString());
                }

                value = val + "";
            }
            cells[index] = new Cell(value, "double", 5 + index);
        }
        return cells;
    }

    private Map getDataAboutWeekReport(String type, String date) {
        Map map = new HashMap();
        try {
            List list = null;
            String[] types = type.split(",");
            if (types[0].equals("one")) {
                list = reportRepository.getSmTwoWeekReportOne(types[1], date);
            } else {
                // list = reportRepository.getWeekReport(type);
            }


            for (Object o : list) {
                Map m = (Map) o;

                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneJan", m.get("oneJan"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneFeb", m.get("oneFeb"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneMar", m.get("oneMar"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneApr", m.get("oneApr"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneMay", m.get("oneMay"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneJun", m.get("oneJun"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneJul", m.get("oneJul"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneAug", m.get("oneAug"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneSep", m.get("oneSep"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneOct", m.get("oneOct"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneNov", m.get("oneNov"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneDeco", m.get("oneDeco"));

            }
        } catch (Exception e) {
            logger.error("SM2B周报表数据查询异常", e);
        }
        return map;
    }
}
